﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MealPlan : System.Web.UI.Page
{
    PIMServices pimService = new PIMServices();
    private int _MealPlanId = 0;
    private string UserIdOfMealPlan = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["MealPlanId"] != null)
        {
            _MealPlanId = Convert.ToInt32(Request.QueryString["MealPlanId"].ToString());
            if (Session["Userid"] != null)
            {
                if (!IsPostBack)
                {
                    fillMealPlan(_MealPlanId);
                    btnEdit.Visible = true;
                    btnUpload.Visible = false;
                    if (Session["Userid"].ToString() == UserIdOfMealPlan)
                    {
                        btnDelete.Visible = true;
                    }
                }
            }
            else
            {
                if (!IsPostBack)
                {
                    fillMealPlan(_MealPlanId);
                }
            }
        }
    }

    private void fillMealPlan(int mealPlanID)
    {
        try
        {
            PIM.API.MealPlan lMealPlan = pimService.GetMealPlanByMealPlanId(mealPlanID.ToString());
            txtMealPlanName.Text = lMealPlan.MealPlanName;
            txtDescription.Text = lMealPlan.Description;
            lblMealPlanName.Text = lMealPlan.MealPlanName;
            lblDescription.Text = lMealPlan.Description;
            hdImage.Value = lMealPlan.MealPlanPhoto;
            UserIdOfMealPlan = lMealPlan.UserID.ToString();
            ratingStar.Value = lMealPlan.MealPlanRating.ToString();
            Checkbox1.Checked = lMealPlan.isShared;
            chIsPublish.Checked = lMealPlan.isShared;
            ScriptManager.RegisterStartupScript(Page, typeof(Page), "Call InitRating", "initRating()", true);
            if (!string.IsNullOrEmpty(lMealPlan.MealPlanPhoto))
            {
                string base64 = lMealPlan.MealPlanPhoto;
                hdImage.Value = base64;
                Image2.Src = "data:image/jpeg;charset=utf-8;base64," + base64;
            }
        }
        catch (Exception ex)
        {

        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(Page, typeof(Page), "Call InitRating", "initRating()", true);
        try
        {
            string userId = "";
            string[] arrayRecipeId = hdRecipeId.Value.Split(',');
            if (Session["Userid"] != null)
                userId = Session["Userid"].ToString();
            PIM.API.MealPlan mealPlan = pimService.GetMealPlanByMealPlanId(_MealPlanId.ToString());
            if (!string.IsNullOrEmpty(userId))
            {
                int _idUser = Convert.ToInt32(userId);
                if (mealPlan.UserID == _idUser)
                {
                    if (string.IsNullOrEmpty(hdImage.Value))
                    {
                        hdImage.Value = mealPlan.MealPlanPhoto;
                    }
                    string result = pimService.UpdateMealPlan(mealPlan.MealPlanID, mealPlan.UserID, mealPlan.CreatedUserID, mealPlan.ParentUserId,
                        txtMealPlanName.Text, mealPlan.isShared, mealPlan.canCopy, mealPlan.NoOfReview, mealPlan.MealPlanRating, mealPlan.AvgCalPerDay,
                        mealPlan.EstCostPerDay, hdImage.Value, txtDescription.Text);
                    Image2.Src = "data:image/jpeg;charset=utf-8;base64," + hdImage.Value;
                    //Update Recipe in MealPlanDetail
                    if (result.Equals("True"))
                    {
                        pimService.DeleteMealPlanDetail(mealPlan.MealPlanID);
                        if (arrayRecipeId != null && arrayRecipeId.Length > 0)
                        {
                            for (int i = 0; i < arrayRecipeId.Length; i++)
                            {
                                if (!string.IsNullOrEmpty(arrayRecipeId[i]))

                                    pimService.InsertPlanItMealDetail(mealPlan.MealPlanID, 1, Convert.ToInt32(arrayRecipeId[i]), 0, 1, 0, DateTime.Now, "", mealPlan.UserID.ToString());
                            }

                            //Insert
                        }
                        Response.Redirect("MyMealPlan.aspx");
                        //ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "Success", "alert('Updated MealPlan Successful')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "Error", "alert('Can not Update MealPlan')", true);
                    }
                }
                else
                {
                    List<PIM.API.MealPlan> lMealPlanOfUser = pimService.GetMealPlan(_MealPlanId.ToString(), _idUser.ToString());
                    if (lMealPlanOfUser != null && lMealPlanOfUser.Count > 0)
                    {
                        PIM.API.MealPlan mealPlanOfUser = lMealPlanOfUser[0];
                        if (string.IsNullOrEmpty(hdImage.Value))
                        {
                            hdImage.Value = mealPlan.MealPlanPhoto;
                        }
                        string result = pimService.UpdateMealPlan(mealPlanOfUser.MealPlanID, mealPlanOfUser.UserID, mealPlanOfUser.CreatedUserID,
                                mealPlanOfUser.ParentUserId, txtMealPlanName.Text, mealPlanOfUser.isShared, mealPlanOfUser.canCopy, mealPlanOfUser.NoOfReview,
                                mealPlanOfUser.MealPlanRating, mealPlanOfUser.AvgCalPerDay,
                            mealPlanOfUser.EstCostPerDay, hdImage.Value, txtDescription.Text);

                        Image2.Src = "data:image/jpeg;charset=utf-8;base64," + hdImage.Value;
                        if (result.Equals("True"))
                        {
                            pimService.DeleteMealPlanDetail(mealPlanOfUser.MealPlanID);
                            if (arrayRecipeId != null && arrayRecipeId.Length > 0)
                            {
                                for (int i = 0; i < arrayRecipeId.Length; i++)
                                {
                                    if (!string.IsNullOrEmpty(arrayRecipeId[i]))
                                        pimService.InsertPlanItMealDetail(mealPlan.MealPlanID, 1, Convert.ToInt32(arrayRecipeId[i]), 0, 1, 0, DateTime.Now, "", mealPlanOfUser.UserID.ToString());
                                }

                                //Insert
                            }
                            Response.Redirect("MyMealPlan.aspx");
                            //ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "Success", "alert('Updated MealPlan Successful')", true);
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "Error", "alert('Can not Update MealPlan')", true);
                        }
                    }
                    else
                    {
                        double rating = 5;
                        if (!string.IsNullOrEmpty(ratingStar.Value))
                            rating = Convert.ToDouble(ratingStar.Value);
                        //Insert New MealPlan
                        int mealPlanId = pimService.InsertPlanItMeal(_idUser, _idUser, _idUser, txtMealPlanName.Text, true, true, 4, 0, 0, hdImage.Value, 0, txtDescription.Text);
                        //Insert Recipe
                        if (mealPlanId > -1)
                        {
                            if (arrayRecipeId != null && arrayRecipeId.Length > 0)
                            {
                                for (int i = 0; i < arrayRecipeId.Length; i++)
                                {
                                    if (!string.IsNullOrEmpty(arrayRecipeId[i]))
                                    {
                                        bool bResult = pimService.InsertPlanItMealDetail(mealPlanId, 1, Convert.ToInt32(arrayRecipeId[i]), 0, 1, 0, DateTime.Now, "", _idUser.ToString());
                                    }
                                }

                                //Insert

                                Response.Redirect("MyMealPlan.aspx");
                            }
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "Error", "alert('Can not Create MealPlan')", true);
                        }

                    }
                }

            }

        }
        catch (Exception ex)
        {

        }
    }


    protected void btn_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(Page, typeof(Page), "Call InitRating", "initRating()", true);
        try
        {
            if (panel2.Visible)
            {
                btnEdit.Text = "Cancel";
                btnUpload.Visible = true;
                //GetRecipeUser(_RecipeId);
                btnSave.Visible = true;
                panel1.Visible = true;
                panel2.Visible = false;

            }
            else
            {
                btnEdit.Text = "Edit";
                btnUpload.Visible = false;
                btnSave.Visible = false;
                panel1.Visible = false;
                panel2.Visible = true;
            }
        }
        catch (Exception ex)
        {

        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(Page, typeof(Page), "Call InitRating", "initRating()", true);
        bool result = false;
        try
        {
            result = Convert.ToBoolean(pimService.DeletMealPlan(_MealPlanId));

        }
        catch (Exception ex)
        {

        }
        if (result)
        {
            Response.Redirect("MyMealPlan.aspx");
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "Error", "alert('Can not Delete MealPlan')", true);
        }
    }
}